package com.torry.replay.infras.repo.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.torry.replay.api.result.GetDragonInfoResult;
import com.torry.replay.infras.entity.DragonDO;
import com.torry.replay.infras.entity.DragonDetailDO;
import com.torry.replay.infras.mapper.DragonDetailMapper;
import com.torry.replay.infras.repo.DragonDetailRepository;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.torry.replay.infras.repo.DragonRepository;
import com.torry.replay.infras.repo.SeatRepository;
import com.torry.replay.infras.util.NumberUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author torry.wang
 * @since 2023-12-11
 */
@Service
public class DragonDetailRepositoryImpl extends ServiceImpl<DragonDetailMapper, DragonDetailDO> implements DragonDetailRepository {

    @Resource
    private DragonRepository dragonRepository;
    @Resource
    protected SeatRepository seatRepository;

    @Override
    public GetDragonInfoResult getDragonDetailsByDragonId(Long dragonId) {
        DragonDO dragonDO = dragonRepository.getById(dragonId);
        if (dragonDO == null) return null;
        List<DragonDetailDO> listSortedByBuyAmount = this.list(new LambdaQueryWrapper<DragonDetailDO>().eq(DragonDetailDO::getDragonId, dragonId).orderByDesc(DragonDetailDO::getBuyAmount));
        List<DragonDetailDO> listSortedBySellAmount = this.list(new LambdaQueryWrapper<DragonDetailDO>().eq(DragonDetailDO::getDragonId, dragonId).orderByDesc(DragonDetailDO::getSellAmount));

        List<DragonDetailDO> buyList = listSortedByBuyAmount.subList(0, listSortedByBuyAmount.size() >= 5 ? 5 : listSortedByBuyAmount.size() - 1);
        List<DragonDetailDO> sellList = listSortedBySellAmount.subList(0, listSortedBySellAmount.size() >= 5 ? 5 : listSortedBySellAmount.size() - 1);

        List<GetDragonInfoResult.DragonDetail> buys = buildDetailList(buyList);
        List<GetDragonInfoResult.DragonDetail> sells = buildDetailList(sellList);

        GetDragonInfoResult result = new GetDragonInfoResult();
        result.setId(dragonDO.getId());
        result.setBuyList(buys);
        result.setSellList(sells);
        result.setName(dragonDO.getName());
        result.setDate(dragonDO.getDate());
        result.setRangePercent(dragonDO.getRangePercent());
        double sumBuy = listSortedByBuyAmount.stream().mapToDouble(DragonDetailDO::getBuyAmount).sum();
        result.setTotalBuyAmount(NumberUtil.format(sumBuy));
        double sumSell = listSortedByBuyAmount.stream().mapToDouble(DragonDetailDO::getSellAmount).sum();
        result.setTotalSellAmount(NumberUtil.format(sumSell));
        result.setTotalNetAmount(NumberUtil.format(sumBuy - sumSell));
        return result;
    }

    public List<GetDragonInfoResult.DragonDetail> buildDetailList(List<DragonDetailDO> list) {
        return list.stream().map(e -> {
            GetDragonInfoResult.DragonDetail detail = new GetDragonInfoResult.DragonDetail();
            detail.setId(e.getId());
            detail.setDragonId(e.getDragonId());
            detail.setSeatId(e.getSeatId());
            detail.setBuyAmount(e.getBuyAmount());
            detail.setSellAmount(e.getSellAmount());
            detail.setSeatName(seatRepository.getById(e.getSeatId()).getSeatName());
            detail.setSeatNickName(seatRepository.getById(e.getSeatId()).getNickName());
            detail.setNetAmount(NumberUtil.format(e.getBuyAmount() - e.getSellAmount()));
            return detail;
        }).collect(Collectors.toList());
    }
}
